﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Business;
using BitPortal.Models;
using BitPortal.Utils;
using System.Collections.ObjectModel;
using System.Collections;
using System.Web.Security;

namespace BitPortal.Admin.Note
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            (this.NoteAddEdit1 as Controls.BaseAdminControl).ReloadControls.Add(this.GridPanel1);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            NoteAddEdit1.BindData(0);
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                NoteAddEdit1.BindData(sm.SelectedRows[0].RecordID.ToInt64TryParse());
            }

        }

        public void LoadGridData()
        {
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
            (this.GridPanel1.SelectionModel.Primary as RowSelectionModel).ClearSelections();
        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            FilterConditions fc = null;
            string s = e.Parameters[this.GridFilters1.ParamPrefix];
            if (!string.IsNullOrEmpty(s))
            {
                fc = new FilterConditions(s);
            }
            List<Models.Note> data = NoteBLL.GetListNodeToView(GeneralUtils.ToInt64TryParse(Request.QueryString["NoteGroupID"]), true);

            Store1.DataSource = ExtNet.GirdData.Filter<Models.Note>(data, fc, e);
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                string unDelete = "";
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Note Note = bitEnt.Notes.SingleOrDefault(ent => ent.NoteID == ID);
                    if (Note != null)
                    {
                        if (Note.CheckPermissonToDelete())
                        {
                            bitEnt.Notes.DeleteObject(Note);
                        }
                        else
                        {
                            unDelete += "  " + Note.NoteID;
                        }
                    }
                }
                int record = bitEnt.SaveChanges();
                ShowNotification("Xóa " + record + " ghi chú thành công!");
                sm.ClearSelections();
                LoadGridData();
                if (!string.IsNullOrEmpty(unDelete)) ShowMessage("Bạn không có quyền xóa ghi chú: " + unDelete + ".");
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        [DirectMethod]
        public void LoadDetail(string id)
        {
            Models.Note n = Business.NoteBLL.GetByID(id.ToInt64TryParse());
            if (n != null)
            {
                lblDatetimeCreate.Text = n.Logs.DatetimeCreated != null ? ((DateTime)n.Logs.DatetimeCreated).ToString("dd/MM/yyyy hh:mm tt") : "Không thông tin";
                lblDatetimeEdit.Text = n.Logs.DatetimeEdited != null ? ((DateTime)n.Logs.DatetimeEdited).ToString("dd/MM/yyyy hh:mm tt") : "Không thông tin";
                lblCreatingUser.Html = n.Logs.CreatingUser != null ? "<a href='#' >" + Membership.GetUser(n.Logs.CreatingUser).UserName + "</a>" : "Không thông tin";
                lblEditingUser.Html = n.Logs.EditingUser != null ? "<a href='#' >" + Membership.GetUser(n.Logs.EditingUser).UserName + "</a>" : "Không thông tin";
                lblPermission.Html = "";
                if (n.CheckPermissonToEdit()) lblPermission.Html += "[ <a href='#'>Sửa</a> ] ";
                if (n.CheckPermissonToDelete()) lblPermission.Html += " [ <a href='#'>Xóa</a> ] ";
                wDetail.Show();
            }
        }

    }
}